package com.javabasic.algorithm.leetcode;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/18 11:36 下午
 * @see
 */
public class ReverseBits {

    /**
     * 通过位运算以及左移右移来获取反转之后的二进制数，注意这里不区分有符号和无符号整数，均以32bit位来处理
     * @param n
     * @return
     */
    public int reverseBits(int n) {
        int len = 32;
        int result = 0;
        while (len > 0) {
            if ((n&1) == 1) {
                result <<= 1;
                result++;
            } else {
                result <<= 1;
            }
            len--;
            n >>= 1;
        }
        return result;
    }
}
